#include "StdAfx.h"
#include "Config.h"
#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

Config::Config(void):frameWidth(640),frameHeight(480),
	searchRadius(30),objectCount(2),initPoint(NULL)
{
}

Config::Config(const std::string& path):frameWidth(640),frameHeight(480),
	searchRadius(30),objectCount(2),initPoint(NULL)
{
	ifstream f(path.c_str());

	if(!f)
	{
		cout << "error: could not load config file: " << path << endl;
		return;
	}

	string line,name,tmp;
	int objectCountTemp = 0;
	while(getline(f,line))
	{
		istringstream iss(line);
		iss >> name >> tmp;

		if(iss.fail() || tmp != "=" || name[0] == '#') continue;

		if (name == "objectCount") 
		{
			iss >> objectCount;
			initPoint = new Point2i[objectCount];
			objectCountTemp = objectCount;
			for (int i=objectCount;i>0;i--)
			{
				initPoint[i].x = 0;
				initPoint[i].y = 0;
			}
		}
		else if(name == "frameWidth") iss >> frameWidth;
		else if(name == "frameHeight") iss >> frameHeight;
		else if(name == "searchRadius") iss >> searchRadius;
		else if(name == "sequenceName") iss >> sequenceName;
		else if(name == "sequenceBasePath") iss>> sequenceBasePath;
		else if (name == "Coordinate")
		{
			if (objectCountTemp > 0)
			{
				iss >> initPoint[objectCountTemp-1].x;
				iss >> initPoint[objectCountTemp-1].y;
				//sscanf(coordinate.c_str(),"%d %d",&initPoint[objectCountTemp-1].x,&initPoint[objectCountTemp--].y);
				objectCountTemp--;
				cout << initPoint[objectCountTemp].x <<"   "<<initPoint[objectCountTemp].y <<endl;
			}
		}	

	}
}

ostream& operator<< (ostream& out, const Config& conf)
{
	out << "config:" << endl;
	out << "objectCount          = " <<conf.objectCount<<endl;
	out << "  sequenceBasePath   = " << conf.sequenceBasePath << endl;
	out << "  sequenceName       = " << conf.sequenceName << endl;
	//out << "  resultsPath        = " << conf.resultsPath << endl;
	out << "  frameWidth         = " << conf.frameWidth << endl;
	out << "  frameHeight        = " << conf.frameHeight << endl;
	out << "  searchRadius       = " << conf.searchRadius << endl;

	for (int i=conf.objectCount;i>0;i--)
		out<< "  "<<conf.objectCount - i<<" initPosition    = "<< conf.initPoint[i-1].x<<"  "<<conf.initPoint[i-1].y<<endl;

	return out;
}

Config::~Config(void)
{
	delete[] initPoint;
}
